package ks.client.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

//import sample.SampleJTable_Tutorial;
//import sample.controllers.AddCarController;
//import sample.controllers.RemoveCarController;
//import sample.model.SampleModel;

import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

//import sample.model.SampleModel;

import ks.client.controllers.AddUserController;
import ks.client.controllers.GetPlayerInfoController;
import ks.client.controllers.PrivateChatClick;
import ks.client.controllers.RemoveUserController;
import ks.client.controllers.SelectTableController;
import ks.client.lobby.Lobby;
import ks.managers.user.User;
import ks.client.gui.models.UserModel;


/**
 * UserManagerGUI
 * 
 * @author gregjoverton
 * @author szutshi
 *
 */
public class UMGUI3 extends javax.swing.JPanel {
	
	/**
	 * Lobby that contains the UMGUI3
	 */
	Lobby lobby;
	
	/** Where input is stored. */
	JTextField textField;
	
	/** 
	 * Manage the model that "backs" the JTable. Different from userList because
	 * Swing needs a proper TableModel.
	 */
	UserModel userModel;
	
	/**
	 * JTable showing the information 
	 * @param args
	 */
	JTable userTable;
	
	public UMGUI3(Lobby lobby) {
		this.lobby = lobby;
		setLayout(new FlowLayout());
		launch();
	}
	
	public void addUser (User u) {
		userModel.add(u);
    }
	
	public void removeUser (String name) {
		userModel.remove(new User(name, "temp"));
	}
	
	public void removeUser (User u) {
		userModel.remove(u);
	}
	
	public javax.swing.JTable getTableUsers(){
		return userTable;
	}
	
	/** Expose the model for access. */
	public UserModel getModel() {
		return userModel;
	}
	
	/**
	 * Launch the frame and set everything up.
	 */
	public void launch() {
	
		//Set the properties of jButton1, the Player Information button
        jButton1 = new javax.swing.JButton();
        jButton1.setFont(new java.awt.Font("Lucida Handwriting", 0, 13)); // NOI18N
        jButton1.setText("Player Information");
        jButton1.addActionListener( new GetPlayerInfoController(lobby));
        
        
		//Set the properties of jButton2, the Private Chat button
        jButton2 = new javax.swing.JButton();
        jButton2.setFont(new java.awt.Font("Lucida Handwriting", 0, 13)); // NOI18N
        jButton2.setText("Private Chat");
        jButton2.addActionListener( new PrivateChatClick(lobby));
        
        
		//Set the properties of jButton3, the Select Table button
        jButton3 = new javax.swing.JButton();
        jButton3.setFont(new java.awt.Font("Lucida Handwriting", 0, 13)); // NOI18N
        jButton3.setText("Select Player's Table");
        jButton3.addActionListener( new SelectTableController(lobby));
        
        jScrollPane1 = new javax.swing.JScrollPane();
        setBackground(new java.awt.Color(255, 0, 217));
        
		//Set the properties of the table of users
//        userTable.setBackground(new java.awt.Color(102, 0, 102));
//        userTable.setFont(new java.awt.Font("Lucida Handwriting", 0, 13)); // NOI18N
//        userTable.setForeground(new java.awt.Color(255, 0, 217));
                
		JPanel p = new JPanel ();
		p.setLayout(new FlowLayout());
		
		// Just add the JTable to the set. First create the raw list of cars,
		// then the SwingModel that supports the JTable which you now create.
		userModel = new UserModel();
		userTable = new JTable(userModel);
        
        //Set userTable column behavior
        jScrollPane1.setViewportView(userTable);
        userTable.getColumnModel().getColumn(0).setResizable(false);
        userTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        userTable.getColumnModel().getColumn(1).setResizable(false);
        userTable.getColumnModel().getColumn(2).setResizable(false);
        userTable.getColumnModel().getColumn(3).setResizable(false);
        userTable.getColumnModel().getColumn(3).setPreferredWidth(40);        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(58, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addGap(12, 12, 12))
        );
		
		// let's tell the JTable about its columns.
		TableColumnModel columnModel = new DefaultTableColumnModel();
		
		// must build one by one...
		String[] headers = new String[] {"ICON", "USERNAME", "RATING"};
		int index = 0;
		for (String h : headers) {
			TableColumn col = new TableColumn(index++);
			col.setHeaderValue(h);
			columnModel.addColumn(col);
		}		
		userTable.setColumnModel(columnModel);
		
		
		User testUser = new User ("Greg", "private");
		testUser.setRating(999);
		testUser.setIconName("RAWR!");
		userModel.add(testUser);
		userModel.remove(testUser);
	}
	
	// invoke the GUI-based controllers.
	public void add_button_pressed(ActionEvent ae) {
		new AddUserController(this).add(new User("Scary", "Greg"));
	}
	
	// invoke GUI-based controller
	public void remove_button_pressed(ActionEvent ae) {
		new RemoveUserController(this).remove("Scarey");
	}

	public boolean refreshTable() {
		// THIS is the key method!!!
		userTable.revalidate();
		return true;
	}
	
	public String selectedPlayer(){	
		int sRow = userTable.getSelectedRow();
		Object s2 = userTable.getValueAt(sRow, 2);
		String splayer = s2.toString();
		return splayer;
		}
	

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration


}
